package com.stylefeng.guns.modular.vpn.mquartz;

import com.stylefeng.guns.modular.vpn.timer.DisConnectionQuartzJob;
import com.stylefeng.guns.modular.vpn.timer.StatisticsQuartzJob;
import com.stylefeng.guns.modular.vpn.utils.MQuartzUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description 任务管理器
 * @Author Mr.God
 * @Date 2020-03-15 17:07
 **/
@Service
@Slf4j
public class QuartzManager {
    @Autowired
    @Qualifier("scheduler")
    private Scheduler scheduler;

    /**
     * 统计业务数据任务
     * @param jobName
     * @param jobGroupName
     * @param triggerName
     * @param triggerGroupName
     * @param croExpression
     * @throws SchedulerException
     */
    public void addCroJob(String jobName,String jobGroupName
            ,String triggerName,String triggerGroupName,
                                 String croExpression) throws SchedulerException {
        //创建JobDetail，指定作业名和组，以及具体的类名
        JobDetail jobDetail = JobBuilder.newJob(StatisticsQuartzJob.class)
                .withIdentity(jobName,jobGroupName).build();
        setCronSchedule(jobDetail,triggerName,triggerGroupName,croExpression);
//        //表达式调度构建器(即任务执行的时间)
//        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(croExpression);
//        //按新的cronExpression表达式构建一个新的trigger
//        CronTrigger trigger = TriggerBuilder.newTrigger()
//                .withIdentity(triggerName,triggerGroupName).withSchedule(scheduleBuilder).build();
//
//        Date ft = scheduler.scheduleJob(jobDetail,trigger);
//
//        System.out.printf("%s 这个任务会在 %s 准时开始运行",
//                jobDetail.getKey(), ft.toLocaleString());
//        log.info("{} 这个任务会在 {} 准时开始运行",
//                jobDetail.getKey(), ft.toLocaleString());
//        if(!scheduler.isShutdown()){
//            scheduler.start();
//        }
    }


    public void disConnectJob(String jobName,String jobGroupName
            ,String triggerName,String triggerGroupName,
                          String croExpression) throws SchedulerException {
        //创建JobDetail，指定作业名和组，以及具体的类名
        JobDetail jobDetail = JobBuilder.newJob(DisConnectionQuartzJob.class)
                .withIdentity(jobName,jobGroupName).build();
        setCronSchedule(jobDetail,triggerName,triggerGroupName,croExpression);
    }

    private void setCronSchedule(JobDetail jobDetail,String triggerName,String triggerGroupName,
                                 String croExpression) throws SchedulerException {
        //表达式调度构建器(即任务执行的时间)
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(croExpression);
        //按新的cronExpression表达式构建一个新的trigger
        CronTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(triggerName,triggerGroupName).withSchedule(scheduleBuilder).build();

        Date ft = scheduler.scheduleJob(jobDetail,trigger);

        System.out.printf("%s 任务会在 %s 准时开始运行",
                jobDetail.getKey(), ft.toLocaleString());
        log.info("{} 任务会在 {} 准时开始运行",
                jobDetail.getKey(), ft.toLocaleString());
        if(!scheduler.isShutdown()){
            scheduler.start();
        }
    }

    /**
     * 暂停定时任务
     * @param name
     * @throws Exception
     */
    public void pauseJob(String name) throws Exception{
        JobKey jobKey = new JobKey(name,name);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if(jobDetail == null){

        }
        scheduler.pauseJob(jobKey);
    }
}
